package cn.tyoui.gui;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.wltea.analyzer.lucene.IKAnalyzer;
import cn.tyoui.bean.DataBean;
import cn.tyoui.mysql.ConnDataSoure;

public class LogicGUI {

    static ConnDataSoure connDataSoure = ConnDataSoure.SingleConn();

    public static List<DataBean> vagueFind(String string) {
        connDataSoure.setSql("select * from loopholedata where loopholename like ?");
        List<DataBean> list = connDataSoure.readerSouce("%" + string + "%");
        return list;
    }

    public static List<DataBean> accuarteFind(String string) {
        connDataSoure.setSql("select * from loopholedata where loopholename=(?)");
        List<DataBean> list = connDataSoure.readerSouce(string);
        return list;
    }

    public static List<DataBean> AnalyzerFind(String text) {
        List<DataBean> list = new ArrayList<>();
        Analyzer analyzer = new IKAnalyzer();
        try {
            TokenStream tokenStream = analyzer.tokenStream("title", text);
            tokenStream.addAttribute(CharTermAttribute.class);
            tokenStream.reset();
            while (tokenStream.incrementToken()) {
                CharTermAttribute termAttribute = tokenStream.getAttribute(CharTermAttribute.class);
                String work = termAttribute.toString();
                if (work.length() > 1)
                    list.addAll(vagueFind(work));
            }
        } catch (Exception e) {
        }
        analyzer.close();
        repeats(list);
        return list;
    }

    private static void repeats(List<DataBean> list) {
        Collections.sort(list);
        for (int i = 0; i < list.size() - 1; i++)
            if (list.get(i).getCNNVD_ID().equals(list.get(i + 1).getCNNVD_ID())) {
                list.remove(list.get(i + 1));
                i--;
            }
    }
}
